<?php
namespace App\Http\Controllers\Admin;


use App\Models\Admin;
use App\Models\ChannelSource;
use App\Models\Member;
use App\Models\Role;
use Illuminate\Http\Request;

/**
 * banner
 *
 * @author Miluo Huang
 */
class UserController extends Controller
{

	/**
	 * 管理员列表
	 */
	public function showAdminList(Request $request)
	{
		$data = Admin::paginate();
		return view('admin.admin.list')->withData($data);
	}

	/**
	 * 管理员编辑
	 */
	public function showAdminEdit(Request $request){
		$data = Admin::find($request->input('id'));
		// 取得系统角色列表。
		$roles = Role::all();
        $channel_ids = explode(',',$data->channel);
        $channel_ids = array_unique($channel_ids);
        $channel = ChannelSource::whereIn('id',$channel_ids)->get();
		return view('admin.admin.edit')->withData($data)->withChannel($channel)
				->withRoles($roles);
	}

	/**
	 * 管理员保存
	 */
	public function saveAdminEdit(Request $request){
		// 验证输入。
		$this->validate($request, [
			'username' => 'required|unique:admins,username,' . $request->input('id', 0),
			'password' => 'between:4,16|confirmed',
		],[
			'username.required' => '用户名必须填！',
			'username.unique' => '用户名已经存在！',
			'password.between' => '密码长度必须在4-16位！',
			'password.confirmed' => '两次密码不一致！',
		]);

		// 取得要编辑的模型。
		$data = $request->has('id') ? Admin::find($request->input('id')) : null;
		if (is_null($data)) {
			$data = new Admin();
		}

		// 编辑数据。
		$data->username = $request->input('username');
		if ($request->has('password')) {
			$data->password = $request->input('password');
		}
		$data->realname = $request->input('realname');
		$request->has('avatar_id') ? $data->avatar_id = $request->input('avatar_id') : '';
        $data->channel = $request->has('channel_ids') ? ','.implode(',',$request->input('channel_ids')).',' : '';
		$data->save();

		// 刷新角色表。
		$data->roles()->sync($request->input('roles',[]));
		// 返回成功信息。
		return redirect()->back()->withMessageSuccess('保存成功。');
	}

	/**
	 * 用户列表
	 */
	public function showMemberList(Request $request){
		$data = Member::orderBy('id','desc');
		if($request->has('nickname')){
			$data = $data->where('nickname','like','%'.$request->input('nickname').'%');
		}
		if($request->has('mobile')){
			$data = $data->where('mobile',$request->input('mobile'));
		}
		$data = $data->paginate();
		$data->appends($request->only(['nickname','mobile']));
//		$status = Member::$status;
		return view('admin.member.list')->withData($data)->withGet($request->all());//->withStatus($status);
	}

	/*
	 * 用户编辑
	 */
	public function showMemberEdit(Request $request){
		$data = Member::find($request->input('id'));
		$status = Member::$status;
		$sex = Member::$sex;
		return view('admin.member.edit')->withData($data)->withStatus($status)->withSex($sex);
	}

	/**
	 * 用户信息保存
	 */
	public function saveMemberEdit(Request $request){
		// 验证输入。
		$this->validate($request, [
			'mobile' => 'required'
		],[
			'mobile.required' => '号码不能为空'
		]);
		$data = $request->has('id') ? Member::find($request->input('id')) : new Member();
		if($request->has('id') && $request->input('mobile') != $data->mobile){
			return redirect()->back()->withMessageError('不能修改号码。');
		}
		$data->nickname = $request->input('nickname');
		$data->integral = $request->input('integral');
		$data->avatar_id = $request->input('avatar_id');
		$data->signature = $request->input('signature');
		$data->sex = $request->input('sex');
		$data->status = $request->input('status');
		$data->mobile = $request->input('mobile');
		$data->save();
		// 返回成功信息。
		return redirect()->back()->withMessageSuccess('保存成功。');
	}
}
